﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SalePartial.Utility;
using System.Web.Security;
using SalePartial.BLL;
using XunPan.Web.Globals;
using SalePartial.Model;
using System.Data.SqlClient;

public partial class Trade_Events_event_list : System.Web.UI.Page
{
    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack)
        {

            ////■■■■■テストため■■■■
            //UserBLL.StoreUserInfoToSession("j@j10.com");
            LoginUserType type = RoleControlManager.checkUserAuthority(User.Identity.Name);
            if (type == LoginUserType.FREE_SUPPLY
                || type == LoginUserType.CERT_SUPPLY || type == LoginUserType.CERT_SUP_PUR
                || type == LoginUserType.FREE_SUP_PUR )
            {

                if (Session["s_userID_guid"] == null || Session["s_companyID_guid"] == null)
                {
                    bool result = UserBLL.StoreUserInfoToSession(User.Identity.Name);
                }

                sdsEventsList.SelectParameters["VendorCompanyID"].DefaultValue = Session["s_companyID_guid"].ToString();
                sdsFilterList.SelectParameters["CompanyID"].DefaultValue = Session["s_companyID_guid"].ToString();
                sdsEventsList.FilterParameters["Filter1"].DefaultValue = "VendorCompanyID='" + sdsEventsList.SelectParameters["VendorCompanyID"].DefaultValue + "'";

                XPInquiryFilter inqFilter = new XPInquiryFilter();
                inqFilter.updateXPFilterCnts(new Guid(Session["s_companyID_guid"].ToString()));

                string displaytext = rpbNavigator.FindItemByValue("All").Text;

                for (int i = 0; i <= 8; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("All").Text = displaytext;

                displaytext = rpbNavigator.FindItemByValue("Specific").Text;
                for (int i = 0; i <= 8; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("Specific").Text = displaytext;

                displaytext = rpbNavigator.FindItemByValue("Important").Text;
                for (int i = 0; i <= 8; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("Important").Text = displaytext;

                displaytext = rpbNavigator.FindItemByValue("Unquote").Text;
                for (int i = 0; i <= 7; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("Unquote").Text = displaytext;

                displaytext = rpbNavigator.FindItemByValue("UnGrant").Text;
                for (int i = 0; i <= 5; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("UnGrant").Text = displaytext;

                displaytext = rpbNavigator.FindItemByValue("Uncomment").Text;
                for (int i = 0; i <= 5; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("Uncomment").Text = displaytext;

                displaytext = rpbNavigator.FindItemByValue("ExpiredToday").Text;
                for (int i = 0; i <= 8; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("ExpiredToday").Text = displaytext;

                displaytext = rpbNavigator.FindItemByValue("ExpiredTomm").Text;
                for (int i = 0; i <= 8; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("ExpiredTomm").Text = displaytext;

                displaytext = rpbNavigator.FindItemByValue("QuoteSent").Text;
                for (int i = 0; i <= 8; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("QuoteSent").Text = displaytext;

                displaytext = rpbNavigator.FindItemByValue("Granted").Text;
                for (int i = 0; i <= 8; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("Granted").Text = displaytext;

                displaytext = rpbNavigator.FindItemByValue("Successful").Text;
                for (int i = 0; i <= 8; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("Successful").Text = displaytext;


                InitializeEventRecordsOnMenuPanel();

                //FormsAuthentication.SetAuthCookie("xylibra@tom.com", False)

            }
            else
            {
                Response.Redirect(GlobalSetting.UrlHomePage, true);

            }

        }

    }

 

    protected void radGridMainlist_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        try
        {
            switch (e.CommandName.ToLower())
            {
                case "changeflag":
                   
                    sdsUpdateEventFlag.UpdateParameters["Importance"].DefaultValue = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Importance"].ToString();
                    sdsUpdateEventFlag.UpdateParameters["IsFlagged"].DefaultValue = ((bool)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IsFlagged"] == false ? "true" : "false");
                    sdsUpdateEventFlag.UpdateParameters["EventID"].DefaultValue = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EventID"].ToString();

                    sdsUpdateEventFlag.Update();
                    InitializeEventRecordsOnMenuPanel();
                    break;
                case "changeimportance":
                    sdsUpdateEventFlag.UpdateParameters["IsFlagged"].DefaultValue = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IsFlagged"].ToString();
                    sdsUpdateEventFlag.UpdateParameters["Importance"].DefaultValue = (e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Importance"].ToString()=="1"?"0":"1");
                    sdsUpdateEventFlag.UpdateParameters["EventID"].DefaultValue = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EventID"].ToString();

                    sdsUpdateEventFlag.Update();
                    InitializeEventRecordsOnMenuPanel();
                    break;
                case "viewdetails":
                    string read = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EventStatus"].ToString().Substring(0, 1);
                    string eventid = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EventID"].ToString();

                    if (read.Equals("0"))
                    {
                        if (GlobalUtility.IsValidGuid(eventid))
                        {
                            XPInquiryEvent xpInqu = new XPInquiryEvent();
                            XPInquiryEventInfo xpevent = xpInqu.GetXPInquiryEvent(new Guid(eventid));
                            xpevent.EventStatus = xpevent.EventStatus.Remove(0, 1);
                            xpevent.EventStatus = xpevent.EventStatus.Insert(0, "1");
                            xpInqu.Update(xpevent);

                        }
                    }
                    HttpContext.Current.Session["EventID"] = eventid;
                    Response.Redirect(e.CommandArgument.ToString(), false);

                    break;
                default:

                    break;
            }


        }
        catch (Exception ex)
        {
            e.Canceled = true;
        }
        e.Item.OwnerTableView.Rebind();

    }

    protected void gvFilterList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState.ToString().Equals("Alternate, Edit"))
        {
           string filterid =  gvFilterList.DataKeys[e.Row.RowIndex]["InquiryFilterID"].ToString();
           //HttpContext.Current.Items.Add("InquiryFilterID", filterid);
           Session["InquiryFilterID"] = filterid;
           Response.Redirect("~/Trade/Supplier/SupplierRetrieval.aspx");

            TextBox box = e.Row.Cells[2].Controls[0] as TextBox;
            box.Width = new Unit(400);
        }
    }

    protected void rpbNavigator_ItemClick(object sender, Telerik.Web.UI.RadPanelBarEventArgs e)
    {
        if (e.Item.Items.Count > 0)
        {
            //主菜单框

        }
        else
        {
            //条目框
            if (e.Item.Value != null && !string.IsNullOrEmpty(e.Item.Value))
            {
                //这里将对数据集进行拣选
                switch (e.Item.Value)
                {
                    case "All":
                        sdsEventsList.FilterParameters["Filter1"].DefaultValue = "VendorCompanyID='" + sdsEventsList.SelectParameters["VendorCompanyID"].DefaultValue+ "'";

                        break;

                    case "Specific":
                        sdsEventsList.FilterParameters["Filter1"].DefaultValue = "VendorCompanyID='" + sdsEventsList.SelectParameters["VendorCompanyID"].DefaultValue+ "' and [IsPublic] = 0";
                        break;
                    
                    case "Important":
                        sdsEventsList.FilterParameters["Filter1"].DefaultValue = "VendorCompanyID='" + sdsEventsList.SelectParameters["VendorCompanyID"].DefaultValue +"' and [Importance] = 1";
                        break;
                    
                    case "Unquote":
                        sdsEventsList.FilterParameters["Filter1"].DefaultValue = "VendorCompanyID='" + sdsEventsList.SelectParameters["VendorCompanyID"].DefaultValue+"' and SUBSTRING([EventStatus], 3, 1) = '0'";
                        break;
                    case "UnGrant":
                        sdsEventsList.FilterParameters["Filter1"].DefaultValue = "VendorCompanyID='" + sdsEventsList.SelectParameters["VendorCompanyID"].DefaultValue+ "' and SUBSTRING([EventStatus], 2, 2) = '01'";
                        break;
                    case "Uncomment":
                        sdsEventsList.FilterParameters["Filter1"].DefaultValue = "VendorCompanyID='" + sdsEventsList.SelectParameters["VendorCompanyID"].DefaultValue+ "' and SUBSTRING([EventStatus], 2, 3) = '110'";
                        break;

                    case "ExpiredToday":
                        sdsEventsList.FilterParameters["Filter1"].DefaultValue = "VendorCompanyID='" + sdsEventsList.SelectParameters["VendorCompanyID"].DefaultValue +"' and [ExpireDate] = '" + System.DateTime.Today.ToString("d") + "'";
                        break;
                   case "ExpiredTomm":
                        sdsEventsList.FilterParameters["Filter1"].DefaultValue = "VendorCompanyID='" + sdsEventsList.SelectParameters["VendorCompanyID"].DefaultValue+"' and [ExpireDate] = '" + System.DateTime.Today.AddDays(1).ToString("d") + "'";
                        break;


                   case "QuoteSent":
                        sdsEventsList.FilterParameters["Filter1"].DefaultValue = "VendorCompanyID='" + sdsEventsList.SelectParameters["VendorCompanyID"].DefaultValue+"' and SUBSTRING([EventStatus], 3, 1) = '1'";
                        break;
                   case "Granted":
                        sdsEventsList.FilterParameters["Filter1"].DefaultValue = "VendorCompanyID='" + sdsEventsList.SelectParameters["VendorCompanyID"].DefaultValue+"' and SUBSTRING([EventStatus], 2, 2) = '11'";
                        break;
                   case "Successful":
                        sdsEventsList.FilterParameters["Filter1"].DefaultValue = "VendorCompanyID='" + sdsEventsList.SelectParameters["VendorCompanyID"].DefaultValue+ "' and SUBSTRING([EventStatus], 4, 1) = '1'";
                        break;
                    
                    default:

                        return;

                }
                radGridMainlist.Rebind();

            }
        }

    }

    private void InitializeEventRecordsOnMenuPanel()
    {
        //    '初始化所有数据记录数
        string cmdtxt = sdsEventsList.SelectCommand;

        DataView dv = (DataView)sdsEventsList.Select(DataSourceSelectArguments.Empty);


        //Dim da As New SqlDataAdapter(New SqlCommand(sdsEventsList.SelectCommand, New SqlConnection(sdsEventsList.ConnectionString)))
        //Dim dt As New DataTable
        //da.Fill(dt)
        //da.Dispose()

        //Dim dv As DataView = dt.DefaultView


        string displaytext = rpbNavigator.FindItemByValue("All").Text;

        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        displaytext = displaytext + dv.Count.ToString();
        dv.RowFilter = "[EventStatus] LIKE '0%'";
        displaytext = displaytext + "(" + dv.Count.ToString() + "未读)";
        rpbNavigator.FindItemByValue("All").Text = displaytext;


        dv.RowFilter = "[IsPublic] = 0";
        displaytext = rpbNavigator.FindItemByValue("Specific").Text;
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        displaytext = displaytext + dv.Count.ToString();
        dv.RowFilter = dv.RowFilter + "AND [EventStatus] LIKE '0%'";
        displaytext = displaytext + "(" + dv.Count.ToString() + "未读)";
        rpbNavigator.FindItemByValue("Specific").Text = displaytext;


        dv.RowFilter = "[Importance] = 1";
        displaytext = rpbNavigator.FindItemByValue("Important").Text;
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("Important").Text = displaytext;

        //dv.RowFilter = "[QuoteID] = '" + Guid.Empty.ToString() + "'";
        dv.RowFilter = "SUBSTRING([EventStatus], 3, 1) = '0'";
        displaytext = rpbNavigator.FindItemByValue("Unquote").Text;
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("Unquote").Text = displaytext;

        dv.RowFilter = "SUBSTRING([EventStatus], 2, 2) = '01'";
        displaytext = rpbNavigator.FindItemByValue("UnGrant").Text;
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("UnGrant").Text = displaytext;

        dv.RowFilter = "SUBSTRING([EventStatus], 2, 3) = '110'";
        displaytext = rpbNavigator.FindItemByValue("Uncomment").Text;
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("Uncomment").Text = displaytext;

        dv.RowFilter = "[ExpireDate] = '" + System.DateTime.Today.ToString("d") + "'";
        displaytext = rpbNavigator.FindItemByValue("ExpiredToday").Text;
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("ExpiredToday").Text = displaytext;

        dv.RowFilter = "[ExpireDate] = '" + System.DateTime.Today.AddDays(1).ToString("d") + "'";
        displaytext = rpbNavigator.FindItemByValue("ExpiredTomm").Text;
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("ExpiredTomm").Text = displaytext;


        dv.RowFilter = "SUBSTRING([EventStatus], 3, 1) = '1'";
        displaytext = rpbNavigator.FindItemByValue("QuoteSent").Text;
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("QuoteSent").Text = displaytext;

        dv.RowFilter = "SUBSTRING([EventStatus], 2, 2) = '11'";
        displaytext = rpbNavigator.FindItemByValue("Granted").Text;
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("Granted").Text = displaytext;


        dv.RowFilter = "SUBSTRING([EventStatus], 4, 1) = '1'";
        displaytext = rpbNavigator.FindItemByValue("Successful").Text;
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("Successful").Text = displaytext;

    }

}